package com.umakr.ax.app.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.PluginCouponMapper;
import com.umakr.ax.common.model.PluginCoupon;
import com.umakr.ax.common.model.PluginCouponVerPeple;
import com.umakr.ax.common.service.PluginCouponService;
import com.umakr.ax.common.service.PluginCouponVerPepleService;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import com.umakr.ax.utils.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Map;

@Service
public class PluginCouponWebService extends BaseService<PluginCouponMapper, PluginCoupon> {
    @Autowired
    PluginCouponService pluginCouponService;
    @Autowired
    PluginCouponVerPepleService pepleService;
    @Autowired
    Environment env;

    public Object delPluginCoupon(Long id, Integer delStatus) {
        return pluginCouponService.delPluginCoupon(id,delStatus);
    }

    public Object listPluginCoupon(Long rsId) {
        Page<?> page = baseMapper.listPluginCouponForWeb(rsId);
        return page;
    }

    public Object listPluginCouponByTitle(Long rsId,String title) {
        Page<?> page = baseMapper.listPluginCouponByTitle(rsId,title);
        return page;
    }

    public Object getPluginCouponById(Long pcId) {
        String aliUrl = env.getProperty("oss.img.path");
        PluginCoupon pc = queryById(pcId);
        SimpleDateFormat ds = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Map<String,Object> pcMap = Maps.newHashMap();
        pcMap.put("id",pc.getId());
        pcMap.put("rsId",pc.getRsId());
        pcMap.put("rpcLogo", FormatPhotoUrlUtil.formatString(aliUrl,pc.getRpcLogo()));
        pcMap.put("rpcName",pc.getRpcName());
        pcMap.put("rpcTitle",pc.getRpcTitle());
        pcMap.put("starttime",ds.format(pc.getRpcStarttime()));
        pcMap.put("endtime",ds.format(pc.getRpcEndtime()));
        pcMap.put("rpcUsetime",pc.getRpcUsetime());
        pcMap.put("rpcMaxCount",pc.getRpcMaxCount());
        pcMap.put("rpcCouLimit",pc.getRpcCouLimit());
        pcMap.put("rpcUseDescription",pc.getRpcUseDescription());
        pcMap.put("rpcUseNotes",pc.getRpcUseNotes());
        pcMap.put("checkUserAddedListData",pc.getRpcWriteOff());
        return resultMap(pcMap,1,"");
    }

    public Object savePluginCoupon(PluginCoupon pluginCoupon) {
        String aliUrl = env.getProperty("oss.img.path");
        int errorCode = 0 ;
        String msg = "";
        try {
            Long id = null;
            pluginCoupon.setDelStatus(0);
            pluginCoupon.setRpcLogo(pluginCoupon.getRpcLogo().replace(aliUrl,""));
            if(pluginCoupon.getId() == null){
                id =  baseMapper.savePluginCoupon(pluginCoupon);
                msg = "发布成功";
            }else{
                baseMapper.updatePluginCoupon(pluginCoupon);
                id=pluginCoupon.getId();
                msg = "修改成功";
            }

            JSONArray writeOffArray = JSONArray.parseArray(pluginCoupon.getRpcWriteOff());
            for(Object writeOffObj : writeOffArray){
                JSONObject writeOff = (JSONObject)writeOffObj;
                if(writeOff.get("id")!=null) {
                    PluginCouponVerPeple people = new PluginCouponVerPeple();
                    people.setCouponId(id);
                    people.setHxrTel(writeOff.get("phone") == null ? "" : writeOff.get("phone").toString());
                    people.setHxrId(Long.parseLong(writeOff.get("id").toString()));
                    people.setHxrName(writeOff.get("name") == null ? "" : writeOff.get("name").toString());
                    pepleService.update(people);
                }
            }

            errorCode = 1 ;
            return  resultMap(pluginCoupon,errorCode,msg);
        }catch (Exception e){
            e.printStackTrace();
            errorCode = 0 ;
            msg = "发布失败";
            return  resultMap(errorCode,msg);
        }
    }

    public Object makeTmp(Long couponId){
        String aliUrl = env.getProperty("oss.img.path");
        PluginCoupon pc = queryById(couponId);
        FreeMarkerUtil fm = new FreeMarkerUtil(env.getProperty("coupon.static.html.path"));
        Map<String,Object> map = Maps.newHashMap();
        map.put("host",env.getProperty("coupon.static.html.host"));
        map.put("couponId",couponId);
        map.put("rpcTitle",pc.getRpcTitle());
        map.put("rpcName",pc.getRpcName());
        map.put("logourl", FormatPhotoUrlUtil.formatString(aliUrl,pc.getRpcLogo()));

        fm.make(map,"couponTmp.ftl","coupon" + File.separator + couponId);
        return resultMap(1,"");
    }

}
